package cn.yunyichina.reservation.controller.business;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.yunyichina.reservation.constant.Config;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.yunyichina.provider.framework.dubbo.service.ReservationService;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.reservation.common.ResponseEnum;
import cn.yunyichina.reservation.common.jackson.Jackson;
import cn.yunyichina.reservation.component.qiniu.QiniuBucket;
import cn.yunyichina.reservation.component.qiniu.QiniuService;
import cn.yunyichina.reservation.constant.Console;
import cn.yunyichina.reservation.constant.HttpResponse;
import cn.yunyichina.reservation.controller.BaseResponseController;
import cn.yunyichina.reservation.util.RequestUtil;
import cn.yunyichina.utils.convert.JsonUtils;

/**
 *  前段页面用户：项目
 */
@RestController
@RequestMapping("item")
public class ItemController extends BaseResponseController {

    private static final String RESERVATION_CONSOLE_ITEM_FOR_USER_LIST = "yyt.reservation.item.for.user.list";

    private static final String RESERVATION_CONSOLE_ITEM_GET = "yyt.reservation.item.get";
    private static final String RESERVATION_CONSOLE_ITEM_ADD = "yyt.reservation.item.add";
    private static final String RESERVATION_CONSOLE_ITEM_UPDATE = "yyt.reservation.item.update";

    @Resource
    private QiniuService qiniuService;

    @Resource
    private Properties yamlProperties;

    @GetMapping("items")
    public HttpResponse listItem(HttpServletRequest request) {
        Map<String, String> param = RequestUtil.getAllParameters(request);
        Response response = ReservationService.callHumpFromInternal(
        		RESERVATION_CONSOLE_ITEM_FOR_USER_LIST, JsonUtils.toJsonString(param)
        );
        return toHttpResponse(response);
    }

    @GetMapping("item")
    public HttpResponse getItem(HttpServletRequest request) {
        Map<String, String> param = RequestUtil.getAllParameters(request);
        Response response = ReservationService.callHumpFromInternal(
                RESERVATION_CONSOLE_ITEM_GET, JsonUtils.toJsonString(param)
        );
        return toHttpResponse(response);
    }

    @RequestMapping("add")
    public HttpResponse addItem(HttpServletRequest request) {
        Map<String, String> param = RequestUtil.getAllParameters(request);
        param.put("login_user", JsonUtils.toJsonString(this.getConsoleUser((String) request.getAttribute(Console.USERNAME))));
        Response response = ReservationService.callHumpFromInternal(
                RESERVATION_CONSOLE_ITEM_ADD, JsonUtils.toJsonString(param)
        );
        return toHttpResponse(response);
    }

    @RequestMapping("update")
    public HttpResponse updateItem(HttpServletRequest request) {
        Map<String, String> param = RequestUtil.getAllParameters(request);
        param.put("login_user", JsonUtils.toJsonString(this.getConsoleUser((String) request.getAttribute(Console.USERNAME))));
        Response response = ReservationService.callHumpFromInternal(
                RESERVATION_CONSOLE_ITEM_UPDATE, JsonUtils.toJsonString(param)
        );
        return toHttpResponse(response);
    }

//    @PostMapping("upload")
//    public HttpResponse itemDetailImageUpload(@RequestParam("file") MultipartFile file) throws IOException {
//        HttpResponse httpResponse = new HttpResponse();
//        if (!file.isEmpty()) {
//            com.qiniu.http.Response response = qiniuService.upload(file.getBytes(), QiniuBucket.RESERVATION);
//            String domain = yamlProperties.getProperty(Config.QINIU_CUSTOM_DOMAIN);
//            if (response.isOK()) {
//                Map<String, String> map = new HashMap<>();
//                Map<String, String> bodyMap = Jackson.getBeanJsonData(response.bodyString(), Map.class);
//                map.put("file_url", domain + bodyMap.get("hash"));
//                httpResponse.setData(map);
//            } else {
//                httpResponse.failure(response.error);
//            }
//        } else {
//            httpResponse.failure(ResponseEnum.ILLEGAL_PARAMETER.getMode(), ResponseEnum.ILLEGAL_PARAMETER.getMessage());
//        }
//        return httpResponse;
//    }

}
